package com.xbang.attendance.web.action.purchase.placespread;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.demo2do.lighturl.annotation.Action;
import com.xbang.attendance.entity.companygps.companyGps;
import com.xbang.attendance.entity.department.Department;
import com.xbang.attendance.entity.legworktimes.LegworkTimes;
import com.xbang.attendance.entity.order.PurchaseOrder;

import com.xbang.attendance.entity.userinfo.Userinfo;
import com.xbang.attendance.entity.vo.LegworkTimesVO;

import com.xbang.attendance.service.SystemService;
import com.xbang.core.page.Result;
import com.xbang.core.struts.support.SearchActionSupport;

/**
 * @author dev.zhang
 * 
 */
public class Search extends SearchActionSupport {

	private static final Log logger = LogFactory.getLog(Search.class);

	private static final long serialVersionUID = -1120551442020217513L;

	@Autowired
	private SystemService systemService;

	private List<Userinfo> userinfos=new ArrayList<Userinfo>(); 
	
	private List<companyGps> companyinfos=new ArrayList<companyGps>(); 
	
	private List<LegworkTimesVO> list=new ArrayList<LegworkTimesVO>();

	private Map<String,Integer> map=new HashMap<String, Integer>();
	
	private List<Department> departList=new ArrayList<Department>();
	
	private List<Integer> effcountList=new ArrayList<Integer>();

	private List<LegworkTimes> legworktimes;

    private String startDate;
    private String endDate;
    
	

	public String getStartDate() {
		return startDate;
	}


	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}


	public String getEndDate() {
		return endDate;
	}


	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	private Integer id;

	private PurchaseOrder purchaseOrder;

	/**
	 * @return
	 * @throws Exception
	 */
	@Action("/purchase/placespread/search/do")
	public String doSearch() throws Exception {
		
		queryList(startDate ,endDate);
		System.out.println(startDate);
		return "index";
	}


	public void queryList(String startDate ,String endDate) throws Exception{
		
	       //查询所有的职员
			String userhql="from Userinfo Userinfo where 1=1";
			Result uResult=systemService.searchByPage(userhql,searchComponent, page);
			userinfos =uResult.getItems();
			
			//根据职员查出对应的部门名称和有效签到总数
			for (int i = 0; i < userinfos.size(); i++) {
				int deid=Integer.valueOf(userinfos.get(i).getDepartment());
				String userid=userinfos.get(i).getUserId();
				Department dp=(Department) systemService.getObject(Department.class, deid);
				departList.add(dp);
				String sql="select count(*) from legwork_times where user_id='"+userid+"' and SUBSTRING(start_time,1,10)>='"+startDate+"' and SUBSTRING(start_time,1,10)<='"+endDate+"'";
				Integer effcount=systemService.queryForInt(sql, page);
				effcountList.add(effcount);
			}
			
			
			
			//查询所有公司
			String companyhql="from companyGps companyGps where 1=1";
			Result cResult=systemService.searchByPage(companyhql,searchComponent, page);
			companyinfos =cResult.getItems();
			
			//根据公司循环所有行
			for (int i = 0; i < companyinfos.size(); i++) {
				//得到公司id
				String companyId=companyinfos.get(i).getCompanyId();
				//创建一个VO类封装公司名称,开始时间，结束时间和List<map>
				LegworkTimesVO ltVO=new LegworkTimesVO();
				
				//封装每一个行中的所有map    map封装的是userName和签到次数
				List<Integer> rowList=new ArrayList<Integer>();
				
				//循环所有的user 构造所有的列
				for (int j = 0; j < userinfos.size(); j++) {
					String userId=userinfos.get(j).getUserId();
					//查询中间表中某个职员在某个外勤公司签到的次数
					String sql="select count(*) from legwork_times where company_id='"+companyId+"' and user_id='"+userId+"' and SUBSTRING(start_time,1,10)>='"+startDate+"' and SUBSTRING(start_time,1,10)<='"+endDate+"'";
					int count=systemService.queryForInt(sql, page);
					rowList.add(count);
				}
				
				//set  LegworkTimesVO的属性值
				ltVO.setCompanyName(companyinfos.get(i).getCompanyName());			
				ltVO.setCountList(rowList);
				ltVO.setStartDate(startDate);
				ltVO.setEndDate(endDate);
				
				//将LegworkTimesVO添加到封装了所有行的list
				list.add(ltVO);
			}
		}
	    

	

	public List<Userinfo> getUserinfos() {
		return userinfos;
	}


	public void setUserinfos(List<Userinfo> userinfos) {
		this.userinfos = userinfos;
	}


	public List<companyGps> getCompanyinfos() {
		return companyinfos;
	}


	public void setCompanyinfos(List<companyGps> companyinfos) {
		this.companyinfos = companyinfos;
	}


	public List<LegworkTimesVO> getList() {
		return list;
	}


	public void setList(List<LegworkTimesVO> list) {
		this.list = list;
	}


	public Map<String, Integer> getMap() {
		return map;
	}


	public void setMap(Map<String, Integer> map) {
		this.map = map;
	}


	public List<Department> getDepartList() {
		return departList;
	}


	public void setDepartList(List<Department> departList) {
		this.departList = departList;
	}


	public List<Integer> getEffcountList() {
		return effcountList;
	}


	public void setEffcountList(List<Integer> effcountList) {
		this.effcountList = effcountList;
	}


	@Override
	public void resetCustomerSearchCriteria() {
		// TODO Auto-generated method stub

	}



	public Integer getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(Integer id) {
		this.id = id;
	}


	public List<LegworkTimes> getLegworktimes() {
		return legworktimes;
	}


	public void setLegworktimes(List<LegworkTimes> legworktimes) {
		this.legworktimes = legworktimes;
	}


	public static long getSerialversionuid() {
		return serialVersionUID;
	}


	public void setSystemService(SystemService systemService) {
		this.systemService = systemService;
	}



}